import java.io.*;
import java.util.Properties;

/**
 * Author: Daniel Michalski
 * Date: 11.03.13
 */

public class Phonebook  {
    public static void main(String[] args) throws IOException {
        Properties props = new Properties();
        BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));
        String name, number;

        boolean changed = false;

        try (FileInputStream fin = new FileInputStream("ksiazkatel.dat")) {
            if (fin != null) {
                props.load(fin);
            }
        } catch (FileNotFoundException ex) {
            System.out.println("Nie znaleziono pliku");
        } catch (IOException e) {
            System.out.println("Błąd odczytu pliku");
        }

        do {
            System.out.println("Wpisz nowe imie i nazwisko" +
                               " (wpisz 'koniec', aby zakończyć): ");
            name = br.readLine();
            if (name.equals("koniec")) continue;

            System.out.println("Wpisz numer telefonu: ");
            number = br.readLine();

            props.put(name, number);
            changed = true;
        } while (!name.equals("koniec"));

        if(changed) {
            FileOutputStream fout = new FileOutputStream("ksiazkatel.dat");
            props.store(fout, "Książka telefoniczna");
            fout.close();
        }

        do {
            System.out.println("Wpisz imię i nazwisko do wyszukania" +
                               " (wpisz 'koniec', aby zakończyć): ");
            name = br.readLine();
            if (name.equals("koniec")) continue;
            number = (String)props.get(name);
            System.out.println(number);
        } while (!name.equals("koniec"));
    }
}
